package com.sxbbc.bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.core.entity.banks.SysProductType;
import com.sxbbc.common.core.entity.banks.SysPushTags;
import com.sxbbc.common.core.mapper.banks.SysPushTagsMapper;
import com.sxbbc.bank.service.ISysPushTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 推送标签表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-09-15
 */
@Service
public class SysPushTagsServiceImpl extends ServiceImpl<SysPushTagsMapper, SysPushTags> implements ISysPushTagsService {

    private static Logger log = LogManager.getLogger(SysPushTagsServiceImpl.class);


    @Autowired
    private SysPushTagsMapper sysPushTagsMapper;

    @Override
    public RestResponse selSysPushTags(BasePage<SysPushTags> page, SysPushTags sysPushTags, String yyTime) {
        log.info("执行【产品展示】推送标签列表查询接口");
        log.info("分页参数{size"+page.getSize()+"，current"+page.getCurrent()+"，pages"+page.getPages()+"}");
        log.info("对象"+sysPushTags);
        log.info("查询时间"+yyTime);
        QueryWrapper<SysPushTags> wrapper = new QueryWrapper<>();
        if(null!=sysPushTags){
            if(StringUtils.isNotEmpty(sysPushTags.getTagsName())){
                wrapper.like("tags_name",sysPushTags.getTagsName());
            }
            if(StringUtils.isNotEmpty(sysPushTags.getTagsCode())){
                wrapper.like("tags_code",sysPushTags.getTagsCode());
            }
            if (null!=sysPushTags.getStatus()){
                wrapper.eq("status",sysPushTags.getStatus());
            }
            if(null!=yyTime && yyTime.trim().length()>0){
                String [] arr=yyTime.split("~");
                wrapper.between("create_time",arr[0],arr[1]);
            }
        }
        super.page(page,wrapper);
        try {
            return RestFactory.success(page.getRecords(), page.getTotal());
        } catch (Exception e) {
            log.error("【产品展示】推送标签列表查询异常："+e.getMessage());
            throw new ThrowJsonException("列表查询异常");
        }
    }

    @Override
    public com.function.common.RestResponse addSysPushTag(SysPushTags sysPushTags) {
        log.info("执行【产品展示】添加标签");
        log.info("对象"+sysPushTags);
        QueryWrapper<SysPushTags> wrapper = new QueryWrapper<>();
        wrapper.eq("tags_name",sysPushTags.getTagsName()).or().eq("tags_code",sysPushTags.getTagsCode());
        List<SysPushTags> sysPushTagsList = sysPushTagsMapper.selectList(wrapper);
        if (sysPushTagsList.size()>0){
            return GetRest.getFail("该标签或该编码已存在，请勿重复添加");
        }
        boolean saved;
        try {
            saved = super.save(sysPushTags);
        } catch (Exception e) {
            log.error("【产品展示】添加标签异常："+e.getMessage());
            throw new ThrowJsonException("添加标签异常");
        }
        if (!saved) {
            return GetRest.getFail("保存失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    @Override
    public com.function.common.RestResponse updateSysPushTag(SysPushTags sysPushTags) {
        log.info("执行【产品展示】修改标签");
        log.info("对象"+sysPushTags);
        SysPushTags selSysProductType = super.getById(sysPushTags.getId());
        if (null == selSysProductType){
            return GetRest.getFail("该标签不存在");
        }
        QueryWrapper<SysPushTags> wrapper = new QueryWrapper<>();
        wrapper.nested(i ->i.eq("tags_name",sysPushTags.getTagsName()).or().eq("tags_code",sysPushTags.getTagsCode())).ne("id",sysPushTags.getId());
        List<SysPushTags> sysPushTagsList = sysPushTagsMapper.selectList(wrapper);
        if (sysPushTagsList.size()>0){
            return GetRest.getFail("该标签或该编码已存在，请勿重复添加");
        }
        boolean saved;
        try {
            saved = super.updateById(sysPushTags);
        } catch (Exception e) {
            log.error("【产品展示】修改标签异常："+e.getMessage());
            throw new ThrowJsonException("修改标签异常");
        }
        if (!saved) {
            return GetRest.getFail("保存失败");
        }
        return GetRest.getSuccess("保存成功");
    }

}
